package cn.gov.msa.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;

import cn.gov.msa.model.Rule;
import cn.gov.msa.model.RuleTime;
import cn.gov.msa.model.UserDetail;
import cn.gov.msa.service.RuleService;
import cn.gov.msa.util.GsonHelper;
import cn.gov.msa.util.InfoObjUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "/rule", produces = "text/plain;charset=UTF-8")
public class RuleController {
	
	ObjectMapper mapper = new ObjectMapper();
	
	@Autowired
	private RuleService ruleService;
		
	@RequestMapping(value = "/list")
	public String list(@RequestBody JSONObject params, HttpSession session) throws Exception {	

		String result = "";		
		int pageNum = 1;
		int pageSize = 10;
		if(params.has("pageNum"))
		{
			pageNum = params.getInt("pageNum");
			params.remove("pageNum");
		}
		if(params.has("pageSize"))
		{
			pageSize = params.getInt("pageSize");
			params.remove("pageSize");
		}
		// 默认查询当前登录账号的所属机构信息
		if("".equals(params.optString("orgcode"))){
			UserDetail userDetail = (UserDetail) session.getAttribute("userDetail");
			params.put("orgcode", userDetail.getOrgcode());
		}
		Rule rule = GsonHelper.convertEntity(params.toString(), Rule.class);
		List<Rule> ruleList = ruleService.selectData(pageNum, pageSize,rule);		
	    //获取分页信息
        PageInfo page = new PageInfo(ruleList);       
		JSONArray ja = JSONArray.fromObject(ruleList);				
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", ja);				
		result = jsonResult.toString();
		return result;
	}
				
	@RequestMapping(value = "/insertRule")
	public String insetRule(@RequestBody JSONObject param) throws Exception {		
		int result = 0;
		String info = "";
	    int save = 0; 		
	    //得到类型的model数据
	    try{
	    	JSONArray ruleDetail = param.getJSONObject("table").getJSONArray("row");
			List<RuleTime> ruleTimeList = new ArrayList<RuleTime>();
			for (Object ruleTimeObject : ruleDetail) {
				RuleTime ruleTime = mapper.readValue(ruleTimeObject.toString(), RuleTime.class);
				ruleTimeList.add(ruleTime);
			}
			
			//得到类型的model数据
			param.remove("table");
			Rule rule = mapper.readValue(param.toString(), Rule.class);	
		    
		    if(this.ruleService.selectDistinct(rule.getOrgcode(), rule.getItemid(), null) == 0){
		    if(ruleService.saveData(rule, ruleTimeList))
		    	save = 1;
		    }else{
		    	save = -1;    //机构项目重复
		    }
	    }
	    catch(Exception e){
	    	e.printStackTrace();
	    	result = 2;
	    	info = "数据保存异常！";
	    }	    	    
	    JSONObject jsonResult = new JSONObject();
	    if(save > 0){
	    	result = 1;
	    	info = "数据保存成功！";
	    }
	    else if(save == -1){
	    	result = 0;
	    	info = "已存在该项目的预约规则！";
	    }
	    
    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();			
	}
	
	@RequestMapping(value = "/deleteRule")
	public String deleteRule(String ruleId) throws Exception {		
	
		 JSONObject jsonResult = new JSONObject();	
		 
		 ruleService.deleteRule(ruleId);	    
		 jsonResult.put("result", "1");

		return jsonResult.toString();			
	}
	
	
	@RequestMapping(value = "/batchDelete")
	public String batchDelete(@RequestParam("ruleIds")String ruleIds) throws Exception {		
	
		 JSONObject jsonResult = new JSONObject();	
		 
		 if(ruleIds.indexOf(",")==-1) {
			 ruleService.deleteRule(ruleIds);
		 }else {
			 String[] ruleId=ruleIds.split(",");
			 for (String string : ruleId) {
				 ruleService.deleteRule(string);
			}
		 }
		 
		jsonResult.put("result", "1");
		return jsonResult.toString();			
	}
	
	@RequestMapping(value = "/updateRule")
	public String updateRule(@RequestBody JSONObject param) throws Exception {		
		int result = 0;
		String info = "";
	    int save = 0; 		
	    //得到类型的model数据    
	    try{	
	    	JSONArray ruleDetail = param.getJSONObject("table").getJSONArray("row");
			List<RuleTime> ruleTimeList = new ArrayList<RuleTime>();
			for (Object ruleTimeObject : ruleDetail) {
				RuleTime ruleTime = mapper.readValue(ruleTimeObject.toString(), RuleTime.class);
				ruleTimeList.add(ruleTime);
			}
			
			//得到类型的model数据
			param.remove("table");
			Rule rule = mapper.readValue(param.toString(), Rule.class);	
		    
			save = ruleService.updateRule(rule,ruleTimeList);
	    }
	    catch(Exception e){
	    	result = 2;
	    	info = "数据保存异常！";
	    }	    	    
	    JSONObject jsonResult = new JSONObject();
	    if(save > 0){
	    	result = 1;
	    	info = "数据保存成功！";
	    }
	    else{
	    	result = 0;
	    	info = "数据保存失败！";
	    }
	    
    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();			
	}
	
	@RequestMapping(value = "/getRuleTime")
	public String getRuleTime(String ruleId) throws Exception {		
		String result = "";
		String info = "";
		JSONObject jsonResult = new JSONObject();
		try{
			List<RuleTime> list = this.ruleService.getRuleTime(ruleId);		
	
			JSONArray japaram = JSONArray.fromObject(list);
			
			result = list.size() > 0 ? "1" : "0";
			info = list.size() > 0 ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
	
			jsonResult.put("datas", japaram);	
		}
		catch(Exception e){
			result = "2";
			info = InfoObjUtil.Exption_msg;
			e.printStackTrace();
		}
		
		jsonResult.put("result", result);
		jsonResult.put("info", info);	
		
		return jsonResult.toString();
	}			
}
